<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\BaseController;
use App\Transformers\UserTransformer;
use Illuminate\Http\Request;

class UserController extends BaseController
{
    /**
     *  用户个人信息详情
     */
    public function userInfo()
    {
        return $this->response->item(auth('api')->user(), new UserTransformer());
    }

    /**
     * 更新用户信息
     */
    public function updateUserInfo(Request $request)
    {
        $request->validate([
                               'name' => 'required|string|max:16',
                           ]);

        $user = auth('api')->user();
        $user->update($request->all());
        return $this->response->noContent();
    }

    /**
     * 更新头像
     */
    public function updateAvatar(Request $request)
    {
        $request->validate([
                               'avatar' => 'required',
                           ],
            [
                'avatar.required' => '用户头像不能为空',
            ]
        );

        $user = auth('api')->user();
        $user->avatar = $request->input('avatar');
        $user->save();

        return $this->response->noContent();
    }
}
